import 'package:flutter/material.dart';
import 'package:flutter_yd_simple/Project/Home/HomePage.dart';
import 'package:flutter_yd_simple/Project/SignOrder/SignBaseOrder.dart';
import 'package:flutter_yd_simple/Project/User/UserPage.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyApp> {

  var _selectIndex = 0;
  var _pageList = [HomePage(),UserPage()];
  //控制pageView滑动
  PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: '我的项目',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity
      ),
      routes: {
        '/SignBaseOrder':(context) => SignOrderBasePage()
      },
        
      home: Scaffold(
        body: PageView(
          children: _pageList,
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),//禁止滑动
        ),
        bottomNavigationBar: BottomAppBar(
          child: Container(
            height: 50,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                tabbarItem(0,'首页',"lib/Project/Images/TabBar/home_normal.png","lib/Project/Images/TabBar/home_select.png"),
                tabbarItem(1,'我的','lib/Project/Images/TabBar/me_normal.png',"lib/Project/Images/TabBar/me_select.png"),
              ],
            ),
          )
        )
      ),
    );
  }

  Widget tabbarItem (
    int index, String title, String normalImgName, String selectImgName) {
      var imgName = index == _selectIndex ? selectImgName : normalImgName;
      var titleColor = index == _selectIndex ? Colors.red:Colors.brown;
      return GestureDetector(
        onTap: () {
          if(_selectIndex == index) {
              return;
          }
          setState(() {
            // 调用 setState 方法，重新 build
            _pageController.jumpToPage(index);
            // 控制 PageView 显示第一个子页面
            _selectIndex = index;
          });
        },
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 5,horizontal: 20),
          child: Column(
            children: [
              Image.asset(imgName,width: 20,height: 20),
              Text(title,style: TextStyle(color: titleColor))
            ],
          ),
        ),
      );
  }

}

